// 云函数入口文件
const cloud = require('wx-server-sdk')

cloud.init()
const db = cloud.database()
const MAX_LIMIT = 100
// 云函数入口函数
exports.main = async (event, context) => {
  //先取出集合记录总数
  const countResult = await db.collection('todos').count()
  const total = countResult.total
  //分几次取每次最多取出100条数据
  const batchTimes = Math.ceil(total/100)
  const tasks = []
  for (let i=0;i<batchTimes;i++){
    const promise = db.collection('todos').skip(i*MAX_LIMIT).limit(MAX_LIMIT).get()
    tasks.push(promise)
  }
  //等待所有的
  return(await Promise.all(tasks)).reduce((acc,cur)=>{
    return{
      data:acc.data.concat(cur.data),
      errMsg:acc.errMsg
    }
  })
  
}